/*
	Copyright (C) 2010 2011 Rémi Thebault

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <QPainter>
#include <QPaintEvent>
#include <QTextOption>

#include "ganttgrads.h"
#include "procycle.h"
#include "timeaxis.h"


GanttRuler::GanttRuler(ProCycle* app, QWidget *parent) :
    PCChild(app),
    QWidget(parent)
{
}

void GanttRuler::paintEvent(QPaintEvent *event) {
    QPainter painter (this);

    painter.save();
    app()->axis()->updateGrads();

    QRect r (0, 0, width(), height());
    QPen liner (Qt::gray);
    QPen texter (Qt::black);

    painter.fillRect(r, Qt::lightGray);
    painter.setPen(liner);
    painter.drawLine(r.bottomLeft(), r.bottomRight());

    float diff = height() / 6;

    for (auto grad = app()->axis()->begin();
         grad != app()->axis()->end(); ++grad) {

        painter.setPen(texter);
        painter.drawText(
                QPoint(grad->pos+diff+1, height()-diff),
                QString::number(grad->time));

        painter.setPen(liner);
        painter.drawLine(grad->pos, 0, grad->pos, height());
    }

    painter.restore();
}




GanttGrads::GanttGrads(ProCycle* app) :
    PCChild(app)
{
}


QRectF GanttGrads::boundingRect() const {
    return QRectF(0, 0, scene()->width(), scene()->height());
}


void GanttGrads::paint(
        QPainter *painter,
        const QStyleOptionGraphicsItem* /*option*/,
        QWidget*)
{
    painter->save();

    app()->axis()->updateGrads();

    QPen liner (Qt::gray);

    for (auto grad = app()->axis()->begin();
         grad != app()->axis()->end(); ++grad) {

        painter->setPen(liner);
        painter->drawLine(grad->pos, 0, grad->pos, scene()->height());
    }

    painter->restore();
}





